package com.cxyd.modules.job.task.agvTask;

import com.cxyd.modules.api.dto.RcsResultHK;
import com.cxyd.modules.wcs.dto.AgvTrkHkPostDTO;
import com.cxyd.modules.wcs.dto.ApplyPassDTO;
import com.cxyd.modules.wcs.entity.TrkEntity;
import com.cxyd.modules.wcs.entity.UrlMappingEntity;
import com.cxyd.modules.wcs.service.AgvTrkHkService;
import com.cxyd.modules.wcs.service.TrkService;
import com.cxyd.modules.wcs.service.UrlMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Component("hk-task")
@Slf4j
public class HKTask {
    //    创建锁，防止并发情况出现
    private Lock lock = new ReentrantLock();
    //处理海康货架任务
    @Resource
    private TrkService trkService;
    //发送海康货架任务
    @Resource
    private AgvTrkHkService agvTrkHkService;
    @Resource
    private UrlMappingService urlService;
    @Resource
    private RestTemplate restTemplate;

    public void scanTask() {
        lock.lock();
        try {
            final TrkEntity wcsTrk = trkService.selectByDvcNo("HK_AGV");
            if (wcsTrk == null) {
//                log.info("暂无[HK_AGV]搬送的任务!");
                return;
            }
            agvTrkHkService.hkTrkScan(wcsTrk);
        } catch (Exception e) {
            log.error("扫描[HK_AGV]任务异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }

    public void postTask() {
        lock.lock();
        try {
//            优先获取一下路由信息
            UrlMappingEntity url_hk = urlService.getUrlByNo("HK_POST_URL");
            if (url_hk == null || url_hk.getUrlAdr().isEmpty()) {
                log.info("[HK_POST_URL]未配置!");
                return;
            }
//            获取属于海康的[agv_trk_hk]搬送任务
            AgvTrkHkPostDTO agvTrkHK = agvTrkHkService.getPostHkTrk();
            if (agvTrkHK == null) {
//                log.info("暂无[HK_AGV]的搬送任务!");
                return;
            }
            log.info("获取到待下发任务：{}", agvTrkHK);
            RcsResultHK respond = restTemplate.postForObject(url_hk.getUrlAdr(), agvTrkHK, RcsResultHK.class);
            if (respond == null) {
                return;
            }
            log.info("获取海康反馈:code:{},msg:{},reqCode:{}", respond.getCode(), respond.getMessage(), respond.getReqCode());
//            处理下发任务
            agvTrkHkService.agvTrkHandle(agvTrkHK, respond);
        } catch (Exception e) {
            log.error("下发[HK_AGV]搬送任务异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }

    /**
     * 任务继续指令(判断线体是否可取可放)
     */
    public void continueTask() {
        lock.lock();
        try {
            UrlMappingEntity url_hk = urlService.getUrlByNo("HK_CONTINUE_URL");
            if (url_hk == null || url_hk.getUrlAdr().isEmpty()) {
                log.info("[HT_CONTINUE_URL]未在[wcs_url_mapping]表中配置!");
                return;
            }
            ApplyPassDTO continueHkTrk = agvTrkHkService.getContinueHkTrk();
            if (continueHkTrk == null) {
//                log.info("暂无[HT_AGV]的任务继续指令!");
                return;
            }
            RcsResultHK respond = restTemplate.postForObject(url_hk.getUrlAdr(), continueHkTrk, RcsResultHK.class);
            if (respond == null) {
                return;
            }
            log.info("获取海康反馈:code:{},msg:{},reqCode:{}", respond.getCode(), respond.getMessage(), respond.getReqCode());
//            处理下发任务
            agvTrkHkService.agvPassHandle(continueHkTrk, respond);

        } catch (Exception e) {
            log.error("下发任务继续指令异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }
}
